/**
 * 
 */
package com.alkandros.linesmash.ui;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.interfaces.IConstants;
import com.alkandros.linesmash.interfaces.IURLConstants;
import com.alkandros.linesmash.location.ILocationCallBack;
import com.alkandros.linesmash.manager.APIManager;
import com.alkandros.linesmash.manager.AlertManager;
import com.alkandros.linesmash.manager.CustomProgressDialog;
import com.alkandros.linesmash.manager.Utils;
import com.alkandros.linesmash.ui.signup.SignUp;

/**
 * @author Jiju Induchoodan
 *
 */
public class SignIn extends BaseActivity implements OnClickListener,IURLConstants,IConstants,ILocationCallBack{

	private TextView txtSignIn;
	private TextView txtSignUp;
	private ImageView btnFacebook;
	private Intent intent;
	private EditText edtUsername;
	private EditText edtPassword;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signin);
		setHeading(R.string.signin);
	    setLeftButton();
	    setRightButton();
	    initialiseUI();
	    setButtons();
	    
	}
	
	private void initialiseUI()
	{
		txtSignIn   	= (TextView)findViewById(R.id.btn_signin);
		txtSignUp   	= (TextView)findViewById(R.id.btn_signup);
		edtUsername   	= (EditText)findViewById(R.id.edt_username);
		edtPassword   	= (EditText)findViewById(R.id.edt_password);
		btnFacebook     = (ImageView)findViewById(R.id.btn_facebook);
	}
	
	private void setButtons()
	{
		txtSignIn.setOnClickListener(this);
		txtSignUp.setOnClickListener(this);

		/*SelectorManager.setBackground(btnSignIn, 
				SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.red_btn, R.drawable.btn_red_press));
		
		SelectorManager.setBackground(btnSignUp, 
				SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.red_btn, R.drawable.btn_red_press));*/
		
/*		SelectorManager.setBackground(btnFacebook, 
				SelectorManager.getButtonDrawableByScreenCathegory(this, R.drawable.facebook_btn, R.drawable.facebook_btn_pressed));
*/	}
	@Override
	public void onClick(View v) 
	{
		if(v==txtSignIn)
		{
			if (!Utils.checkInternetConnection(SignIn.this)) {
				AlertManager.shorttoastMessage(SignIn.this, getString(R.string.Internet_check));
			}
			
			else if ((edtUsername.getText().toString().trim().equals(""))  || (edtPassword.getText().toString().trim().equals(""))) {
				AlertManager.shorttoastMessage(SignIn.this, getString(R.string.Please_fill_username_password));
			}
			else if (!Utils.isValidEmail(edtUsername.getText().toString())) {
				
				
				AlertManager.shorttoastMessage(SignIn.this, getString(R.string.Please_fill_vaild_email));
			}
			else{
				SigninTask task = new SigninTask();
				task.execute();
			}
			
		
			
		}
		else if(v==txtSignUp)
		{
			intent = new Intent(this,SignUp.class);
			startActivity(intent);
		}
	}
	
	class SigninTask extends AsyncTask<Void, Void, Void>
	{
		private APIManager manager;
		private boolean isException=false;
		private String response;
		private String [] names= {"email","password"};
		private String [] values= {edtUsername.getText().toString(),edtPassword.getText().toString()};
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progress.dismiss();
			if(isException)
			{
				AlertManager.shorttoastMessage(SignIn.this, getString(R.string.common_error_message));
			}
			else
			{
				if(response.contains(ERROR_CODE))
				{
					AlertManager.shorttoastMessage(SignIn.this, getString(R.string.invalid_credentails));
				}
				else
				{
					intent = new Intent(SignIn.this,ActionSelection.class);
					intent.putExtra("signed_in", true);
					startActivity(intent);
				}
			}
			
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progress.show();
			manager = new APIManager(URL_LOGIN);
		}
		@Override
		protected Void doInBackground(Void... params) {
			try 
			{
				response= manager.commonPostData(URL_LOGIN,names,values);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				isException=true;
			}
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.alkandros.linesmash.location.ILocationCallBack#setResult(java.lang.String)
	 */
	@Override
	public void setResult(String address) {
		// TODO Auto-generated method stub
		
	}
}